package com.ittony.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ittony.health.constant.MessageConstant;
import com.ittony.health.domain.SetMeal;
import com.ittony.health.entity.PageResult;
import com.ittony.health.entity.QueryPageBean;
import com.ittony.health.entity.Result;
import com.ittony.health.service.SetMealService;
import com.ittony.health.util.AccessCOSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    @RequestMapping("/findByPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setMealService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id){
        try {
            SetMeal setMeal = setMealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setMeal);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }

    @RequestMapping("/findCheckGroupIdsBySetMealId/{setMealId}")
    public Result findCheckGroupIdsBySetMealId(@PathVariable Integer setMealId){
        try{
           List<Integer> checkGroupIds = setMealService.findCheckGroupIdsBySetMealId(setMealId);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestParam(name="ids",required = false) Integer[] checkGroupIds,@RequestBody SetMeal setMeal) {
        try {
            System.out.println(Arrays.toString(checkGroupIds));
            setMealService.add(checkGroupIds,setMeal);
        } catch (Exception e) {
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/upload")
    public Result uploadImage(@RequestBody MultipartFile imgFile) {
        String originalFilename = imgFile.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String suffixName = originalFilename.substring(lastIndexOf);
//        String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName  = UUID.randomUUID().toString()+suffixName;
        try{
            AccessCOSUtil.uploadImage(fileName, imgFile.getBytes());
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        try{
            setMealService.delete(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    @RequestMapping("/edit/{checkGroupIds}")
    public Result edit(@PathVariable Integer[] checkGroupIds,@RequestBody SetMeal setMeal) {
        try{
            setMealService.edit(checkGroupIds,setMeal);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
